package zdream.control.world.buff;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import zdream.control.foe.mmcommon.HPCapsuleA;
import zdream.control.foe.mmcommon.HPCapsuleB;
import zdream.control.world.MFoe;
import zdream.control.world.MFoeBuff;

/**
 * <p>怪物的掉落物的产生方法
 * <p>一般的怪物被打掉之后会产生掉落物, 可能是螺丝 (货币)、加血的、加能量的等
 * </p>
 *
 * @author Zdream
 * @date 2022-06-13
 * @since 0.0.1
 */
public class SpoilsHolder extends MFoeBuff {
	public static final String CATEGORY = "spoils-holder";
	public static final Random RANDOM = new Random();

	public SpoilsHolder(MFoe foe) {
		super(foe);
	}

	@Override
	public void init() {
		//
	}

	@Override
	public String getCategory() {
		return CATEGORY;
	}

	// xxx.class 几率
	public final List<Item> items = new ArrayList<>();
	public int sum = 0;

	public void addMass(String foeName, int rate) {
		items.add(new Item(foeName, rate));
		sum += rate;
	}

	public String select() {
		if (items.isEmpty() || sum == 0) {
			return null;
		}

		int select = RANDOM.nextInt(sum);
		int a = 0;
		for (Item item : items) {
			a += item.rate;
			if (a >= select) {
				return item.foeName;
			}
		}
		return null;
	}

	public static class Item {
		/**
		 * 获得什么 Foe 的概率. 可以为 null
		 */
		public String foeName;
		/**
		 * 比值
		 */
		public int rate;

		public Item(String foeName, int rate) {
			this.foeName = foeName;
			this.rate = rate;
		}
	}

	/**
	 * 一半几率是空, 一半几率是 HPCapsuleA
	 */
	public static SpoilsHolder createDefault(MFoe owner) {
		if (!owner.type.equals(MFoe.TYPE_FOE) && !owner.type.equals(MFoe.TYPE_ELITE)) {
			return null;
		}

		SpoilsHolder b = new SpoilsHolder(owner);
		b.addMass(null, 3);
		b.addMass(HPCapsuleA.NAME, 1);
		b.addMass(HPCapsuleB.NAME, 2);
		return b;
	}
}
